#import "MobileIm.h"

#import "ClientCoreSDK.h"
#import "ConfigEntity.h"

#import "LocalDataSender.h"
#import "PLoginInfo.h"

#import "ChatBaseEventListener.h"
#import "ChatMessageEventListener.h"
#import "MessageQoSEventListener.h"

@implementation MobileIM

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *) supportedEvents{
  return @[
    @"onLoginResponse",
    @"onLinkClose",
    @"onKickOut",
    @"onReceiveMessage",
    @"onErrorResponse",
    @"messagesLost",
    @"messagesBeReceived"
  ];
}

- (NSDictionary *) constantsToExport{
  return @{
    @"ON_LOGIN_RESPONSE": @"onLoginResponse",
    @"ON_LINK_CLOSE": @"onLinkClose",
    @"ON_KICK_OUT": @"onKickOut",
    @"ON_RECEIVE_MESSAGE": @"onReceiveMessage",
    @"ON_ERROR_RESPONSE": @"onErrorResponse",
    @"MESSAGES_LOST": @"messagesLost",
    @"MESSAGES_BE_RECEIVED": @"messagesBeReceived",
  };
}

+ (BOOL) requiresMainQueueSetup{
  return NO;
}

RCT_EXPORT_METHOD(enableProd){
  [ClientCoreSDK setENABLED_DEBUG: NO];
}

RCT_EXPORT_METHOD(init:(NSString *)domain
                  port:(int)port
              resolver:(RCTPromiseResolveBlock)resolve
              rejecter:(RCTPromiseRejectBlock)reject){
  [ConfigEntity setServerIp: domain];
  [ConfigEntity setServerPort: port];

  [ConfigEntity setSenseMode: SenseMode5S];

  [ClientCoreSDK sharedInstance].chatBaseEvent = [[ChatBaseEventListener alloc] initWithEmitter: self];
  [ClientCoreSDK sharedInstance].chatMessageEvent = [[ChatMessageEventListener alloc] initWithEmitter: self];
  [ClientCoreSDK sharedInstance].messageQoSEvent = [[MessageQoSEventListener alloc] initWithEmitter: self];

  resolve(@"");
}

RCT_EXPORT_METHOD(login:(NSString *)userId
                  token:(NSString *)token
               resolver:(RCTPromiseResolveBlock)resolve
               rejecter:(RCTPromiseRejectBlock)reject){
  PLoginInfo *loginInfo = [[PLoginInfo alloc] init];
  loginInfo.loginUserId = userId;
  loginInfo.loginToken = token;
  
  // * 发送登陆数据包(提交登陆名和密码)
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    int code = [[LocalDataSender sharedInstance] sendLogin: loginInfo];
    resolve([NSNumber numberWithInt: code]);
  });
}

RCT_EXPORT_METHOD(loginWithExtra:(NSString *)userId
                           token:(NSString *)token
                           extra:(NSString *)extra
                        resolver:(RCTPromiseResolveBlock)resolve
                        rejecter:(RCTPromiseRejectBlock)reject){
  PLoginInfo *loginInfo = [[PLoginInfo alloc] init];
  loginInfo.loginUserId = userId;
  loginInfo.loginToken = token;
  loginInfo.extra = extra;

  // * 发送登陆数据包(提交登陆名和密码)
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    int code = [[LocalDataSender sharedInstance] sendLogin: loginInfo];
    resolve([NSNumber numberWithInt: code]);
  });
}

RCT_EXPORT_METHOD(hasLogin:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject){
  BOOL res = [ClientCoreSDK sharedInstance].loginHasInit;
  resolve([[NSNumber alloc] initWithBool: res]);
}

RCT_EXPORT_METHOD(send:(NSString *)content
                  toId:(NSString *)toId
                    fp:(NSString *)fp
                 typeU:(int)typeU
              resolver:(RCTPromiseResolveBlock)resolve
              rejecter:(RCTPromiseRejectBlock)reject){
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    int code = [[LocalDataSender sharedInstance] sendCommonDataWithStr: content 
                                                              toUserId: toId 
                                                                   qos: YES 
                                                                    fp: fp 
                                                             withTypeu: typeU
    ];
    resolve([NSNumber numberWithInt: code]);
  });
}

RCT_EXPORT_METHOD(logout:(RCTPromiseResolveBlock)resolve
                rejecter:(RCTPromiseRejectBlock)reject){
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    int code = [[LocalDataSender sharedInstance] sendLoginout];
    resolve([NSNumber numberWithInt: code]);
  });
}

@end
